package org.java.xianxin.web;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * <p>
 * 接口响应参数主体结构
 * </p>
 *
 * @author 敖燕飞
 * @since 上午11:01 2018/3/12
 */
@ApiModel(value = "响应参数主体", description = "响应参数主体")
public class Response<T extends Object> implements Serializable {
    @ApiModelProperty(value = "响应状态码")
    private int code;
    @ApiModelProperty(value = "响应消息")
    private String msg;
    @ApiModelProperty(value = "总条数(用于列表)")
    private int count = 0;
    @ApiModelProperty(value = "响应内容")
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Response() {
    }

    public Response(CodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMsg();
    }

    public Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Response(int code, String msg, int count, T data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public static <T> Response<T> success() {
        return new Response<T>(CodeEnum.SUCCESS);
    }

    public static <T> Response<T> success(int code, String msg) {
        return new Response<T>(code, msg);
    }

    public static <T> Response<T> success(int code, String msg, int count, T data) {
        return new Response<T>(code, msg, count, data);
    }

    public static <T> Response<T> fail() {
        return new Response<T>(CodeEnum.FAIL);
    }

    public static <T> Response<T> fail(String msg) {
        return new Response<T>(CodeEnum.FAIL.getCode(), msg);
    }

    public static <T> Response<T> fail(int code, String msg) {
        return new Response<T>(code, msg);
    }

    public static <T> Response<T> build(int code, String msg, int count, T data) {
        return new Response<T>(code, msg, count, data);
    }

    public enum CodeEnum {
        /**
         * 成功
         */
        SUCCESS(200, "成功"),
        FAIL(555, "失败"),
        ;

        private int code;
        private String msg;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        CodeEnum(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }
}
